#include "GLee.h"
#include "EffectsPass.h"

using namespace glutil;

void EffectsPass::set2DCamera()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,1,0,1,0.1,10.0);
	glMatrixMode(GL_MODELVIEW);
}

void EffectsPass::setTexture(int textureUnit, FBO * texture)
{
	if(texture == NULL)
		m_textures.erase(textureUnit);
	else
		m_textures[textureUnit] = texture;
}

void EffectsPass::doPass(int viewportWidth, int viewportHeight)
{
	std::map<int,FBO *>::const_iterator it;
	std::map<int,FBO *>::const_iterator end = m_textures.end();
	
	for(it = m_textures.begin(); it!=end; ++it)
	{
		glActiveTexture(GL_TEXTURE0 + it->first);
		it->second->bind();
	}

	if(m_destination != NULL)
		m_destination->beginCapture();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0, 0, viewportWidth, viewportHeight);
	set2DCamera();
	glLoadIdentity();

	m_program->bind();
	glBegin(GL_QUADS);
	glTexCoord2f(0,0);
	glVertex3f(0,0,-1);
	glTexCoord2f(1,0);
	glVertex3f(1,0,-1);
	glTexCoord2f(1,1);
	glVertex3f(1,1,-1);
	glTexCoord2f(0,1);
	glVertex3f(0,1,-1);
	glEnd();
	if(m_destination != NULL)
		m_destination->endCapture();
	m_program->unbind();
}


